<template>
  <view class="app bg-white">
    <!-- 搜索 -->
    <view class="search-box">
      <input
        type="text"
        confirm-type="search"
        v-model="searchKey"
        @confirm="search"
        maxlength="12"
        class="search-input flex-1"
        placeholder="请输入关键字搜索"
      />
      <view class="suffix" @click="search">
        <u-icon size="26" color="#b7b7b7" name="search"></u-icon>
      </view>
    </view>

    <!-- 轮播图 -->
    <view class="mx-5 mt-3" v-if="carouselShow">
      <u-swiper
        :list="carousels"
        :autoplay="true"
        :height="300"
        :interval="5000"
        border-radius="20"
      ></u-swiper>
    </view>

    <view>
      <!-- 分类 -->
      <view class="category-grid">
        <view
          class="category-item"
          v-for="(category, index) in categoryData"
          :key="index"
          @click="gotoCategory(category.id)"
        >
          <image class="category-img" mode="aspectFit" :src="category.logo" />
          <text class="category-name">{{ category.name }}</text>
        </view>
      </view>

      <!-- 数据 -->
      <MaterialList ref="listRef"></MaterialList>
    </view>
  </view>
</template>

<script lang="ts" setup>
  // @ts-ignore
  import MaterialList from './list.vue';
  import { ref } from 'vue';
  import { getCarousel } from '@/api/page/carousel';
  import { getMaterialCategory as getCategoryDataApi } from '@/api/material/index';
  import { report } from '@/utils/exception';
  import type { MaterialCategory } from '@/types/material';

  const carousels = ref([]);
  const carouselShow = ref<boolean>(false);
  const categoryData = ref<MaterialCategory[]>([]);
  const categoryCol = ref<number>(4);
  const searchKey = ref<string>('');
  const listRef = ref();

  let searchKeyTemp = '';
  const search = () => {
    if (searchKey.value == searchKeyTemp) return false;
    searchKeyTemp = searchKey.value;
    listRef.value.reloadData(searchKey.value);
    // loading.value = true;
    // paging.value.reload();
  };

  getCarousel(2)
    .then((res) => {
      carousels.value = res.data.imgs.split(',');
      carouselShow.value = res.data.imgs != '' && res.data.status == 1;
    })
    .catch((err) => {
      report(err);
    });

  const getCategoryData = () => {
    getCategoryDataApi()
      .then((res) => {
        categoryData.value = res.data.list;
      })
      .catch((err) => {
        report(err);
      });
  };
  getCategoryData();

  const gotoCategory = (category_id: number) => {
    uni.navigateTo({ url: `/pages/material/category?category_id=${category_id}` });
  };
</script>

<style lang="scss" scoped>
  .search-box {
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 30rpx 30rpx 0;
    background: #f3f3f3;
    padding: 20rpx 30rpx;
    border-radius: 100rpx;

    .search-input {
      font-size: 26rpx;
      color: #9f9f9f;
    }

    .suffix {
      padding-left: 15rpx;
    }
  }

  .category-grid {
    display: grid;
    grid-template-columns: repeat(v-bind(categoryCol), minmax(0, 1fr));
    column-gap: 15rpx;
    row-gap: 30rpx;
    margin: 30rpx 30rpx 10rpx;

    .category-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      padding-bottom: 20rpx;

      .category-img {
        width: 70rpx;
        height: 70rpx;
      }

      .category-name {
        padding-top: 20rpx;
        font-size: 25rpx;
      }
    }
  }
</style>
